﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>InStr - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The InStr function searches for a given occurrence of a string, from the left or the right." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>InStr</h1>

<p>Searches for a given occurrence of a string, from the left or the right.</p>

<pre class="Syntax">FoundPos := <span class="func">InStr</span>(Haystack, Needle <span class="optional">, CaseSensitive := false, StartingPos := 1, Occurrence := 1</span>)</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>Haystack</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The string whose content is searched.</p>
  </dd>
  
  <dt>Needle</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The string to search for.</p>
  </dd>
  
  <dt>CaseSensitive</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#boolean">整数(布尔值)</a></p>
    <p>If the parameter <em>CaseSensitive</em> is omitted or 0 (false), the search is not case sensitive (the method of insensitivity depends on <a href="StringCaseSense.htm">StringCaseSense</a>); otherwise, the case must match exactly.</p>
  </dd>

  <dt>StartingPos</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>If <em>StartingPos</em> is omitted, it defaults to 1 (the beginning of <em>Haystack</em>). Otherwise, specify 2 to start at the second character, 3 to start at the third, and so on.</p>
    <p>If <em>StartingPos</em> is negative, the search is conducted in reverse (right-to-left), starting at that position from the right. 例如, -1 starts at the last character. If <em>StartingPos</em> is 0 or beyond the length of <em>Haystack</em>, 0 is returned.</p>
    <p>Regardless of the value of <em>StartingPos</em>, the return value is always relative to the first character of <em>Haystack</em>. 例如, the position of "abc" in "123abc789" is always 4.</p>
  </dd>

  <dt>Occurrence</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
    <p>If <em>Occurrence</em> is omitted, it defaults to the first match of the <em>Needle</em> in <em>Haystack</em>. Specify 2 for <em>Occurrence</em> to return the position of the second match, 3 for the third match, etc.</p>
  </dd>

  </dl>
  
<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
<p>This function returns the position of an occurrence of the string <em>Needle</em> in the string <em>Haystack</em>. Position 1 is the first character; this is because 0 is synonymous with "false", making it an intuitive "not found" indicator.</p>

<h2 id="Remarks">备注</h2>
<p><a href="RegExMatch.htm">RegExMatch</a> can be used to search for a pattern (regular expression) within a string, making it much more flexible than InStr. However, InStr is generally faster than RegExMatch when searching for a simple substring.</p>
<p>InStr searches only up to the first binary zero (null-terminator), whereas RegExMatch searches the entire <a href="StrLen.htm">length</a> of the string even if it includes binary zero.</p>

<h2 id="Related">相关</h2>
<p><a href="RegExMatch.htm">RegExMatch</a>, <a href="StringCaseSense.htm">StringCaseSense</a>, <a href="is.htm"><em>Value</em> is <em>Type</em></a></p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExRetValue">
<p><a href="#ExRetValue">#1</a></p>
<pre>MsgBox InStr("123abc789", "abc") <em>; Returns 4</em></pre>
</div>

<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#2</a></p>
</pre>Haystack := "The Quick Brown Fox Jumps Over the Lazy Dog"
Needle := "Fox"
If InStr(Haystack, Needle)
    MsgBox "The string was found."
Else
    MsgBox "The string was not found."</pre>
</div>

<div class="ex" id="ExCaseSens">
<p><a href="#ExBasic">#3</a></p>
<pre>Haystack := "The Quick Brown Fox Jumps Over the Lazy Dog"
Needle := "the"
MsgBox InStr(Haystack, Needle, false, 1, 2) <em>; case insensitive search, return start position of second occurence</em>
MsgBox InStr(Haystack, Needle, true) <em>; case sensitive search, return start position of first occurence, same result as above</em>
</pre>
</div>

</body>
</html>